package com.evenmo.hrms.controller.oldController;

import com.evenmo.hrms.pojo.Dept;
import com.evenmo.hrms.service.HrmsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Retention;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    HrmsService hrmsService;

    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> showAllDepts(Integer pageNumber, Integer pageSize) {

        System.out.println(pageNumber + ":" + pageSize);
        //分页
        PageHelper.startPage(pageNumber, pageSize);
        List<Dept> allDepts = hrmsService.findAllDepts();
        PageInfo pageInfo = new PageInfo(allDepts);
        Map<String, Object> result = new HashMap<>();
        result.put("status", "200");
        result.put("data", pageInfo);
        return result;

    }

    @GetMapping("/info/{id}")
    @ResponseBody
    public Map<String, Object> showDeptInfo(@PathVariable("id") Integer id) {
        System.out.println(id);
        Dept dpet = hrmsService.findByDeptId(id);
        Map<String, Object> result = new HashMap<>();
        if (dpet != null) {
            result.put("status", 200);
            result.put("message", "查到了id为" + id + "的数据");
            result.put("data", dpet);
        } else {
            result.put("status", 2400);
            result.put("message", "没有要查找的用户");
        }
        return result;

    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> addDept(Dept dept) {
        Map<String, Object> result = new HashMap<>();
        hrmsService.addDept(dept);
        result.put("status", 200);
        result.put("message", "添加成功");
        return result;
    }

    // 修改部门信息
    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> updateDept(Dept dept) {

        hrmsService.updateDept(dept);
        Map<String, Object> result = new HashMap<>();
        result.put("status", 200);
        result.put("message", "修改成功");
        return result;
    }

    // 删除部门
    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> deleteDept(Integer id) {
        hrmsService.deleteDept(id);
        Map<String, Object> result = new HashMap<>();
        result.put("status", 200);
        result.put("message", "删除成功");
        return result;
    }

    // 查询部门名称
    @GetMapping("/query")
    @ResponseBody

    public Map<String, Object> queryDepts(Dept dept,    String likeParams,    Integer pageNumber,   Integer pageSize) {

        System.out.println(dept);
        System.out.println(likeParams);
        System.out.println(pageNumber);
        System.out.println(pageSize);

        Map<String, Object> params = new HashMap<>();
        params.put("dept",dept);
        params.put("likeParams", likeParams);

        PageHelper.startPage(pageNumber, pageSize);
        List<Dept> deptByParams = hrmsService.findDeptByParams(params);
        PageInfo pageInfo=new PageInfo(deptByParams);
        Map<String, Object> result = new HashMap<>();
        result.put("stauts", 200);
        result.put("data", pageInfo);
        result.put("message", "查询到了数据");
        return result;
    }


}
